package com.ryder.subject.domain.handler.subject;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.base.Preconditions;
import com.ryder.subject.common.enums.IsDeleteFlagEnum;
import com.ryder.subject.common.enums.SubjectInfoTypeEnum;
import com.ryder.subject.domain.convert.RadioSubjectConvert;
import com.ryder.subject.domain.entity.SubjectAnswerBO;
import com.ryder.subject.domain.entity.SubjectInfoBO;
import com.ryder.subject.domain.entity.SubjectOptionBO;
import com.ryder.subject.infra.basic.entity.SubjectRadio;
import com.ryder.subject.infra.basic.service.SubjectRadioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;

/**
 * 描述: 单选题目的策略类
 *
 * @author Ryder
 * @version 1.0
 * @since 1.0 // 版本号，指明从哪个版本开始
 * 更新日期: 2025/02/05
 */

@Component
public class RadioTypeHandler implements SubjectTypeHandler{

    @Autowired
    private SubjectRadioService subjectRadioService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        //只处理Radio相关的
        return SubjectInfoTypeEnum.RADIO;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //单选题目的插入
        LinkedList<SubjectRadio> subjectRadioList = new LinkedList<>();
        if (subjectInfoBO == null) {
            throw new RuntimeException("参数错误");
        }
        Preconditions.checkArgument(CollectionUtils.isNotEmpty(subjectInfoBO.getOptionList()), "题目选项不能为空");

        subjectInfoBO.getOptionList().forEach(option -> {
            SubjectRadio subjectRadio = RadioSubjectConvert.INSTANCE.convertBoToRadio(option);
            subjectRadio.setSubjectId(subjectInfoBO.getId());
            subjectRadio.setIsDeleted(IsDeleteFlagEnum.UN_DELETED.getCode());
            subjectRadioList.add(subjectRadio);
        });
        //插入数据
        subjectRadioService.batchInsert(subjectRadioList);
    }

    @Override
    public SubjectOptionBO query(Long subjectId) {
        SubjectRadio subjectRadio = new SubjectRadio();
        subjectRadio.setSubjectId(Long.valueOf(subjectId));
        List<SubjectRadio> result = subjectRadioService.queryByCondition(subjectRadio);
        List<SubjectAnswerBO> subjectAnswerBOList = RadioSubjectConvert.INSTANCE.convertEntityToBoList(result);
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOList);
        return subjectOptionBO;

    }
}
